package com.salesmanager.web.admin.controller.user;

import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.salesmanager.core.business.generic.exception.ServiceException;
import com.salesmanager.core.business.system.model.SystemConfiguration;
import com.salesmanager.core.business.system.service.SystemConfigurationService;
import com.salesmanager.license.util.LicenseUtil;
import com.salesmanager.web.utils.DateUtil;

@Controller
public class LoginController {

	@Autowired
	protected SystemConfigurationService systemConfigurationService;

	@RequestMapping(value = "/admin/logon.html", method = RequestMethod.GET)
	public String displayLogin(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!hasLicense()) {
			return "forward:/license/display.html";
		}
		return "admin/logon";

	}

	private boolean hasLicense() {
		boolean result = false;
		SystemConfiguration systemConfiguration;
		try {
			systemConfiguration = systemConfigurationService.getByKey("license");
			if (systemConfiguration != null) {
				String license = systemConfiguration.getValue();
				if (LicenseUtil.checkLicense(license)) {
					return true;
				} else if (LicenseUtil.checkTrailLicense(license)){
					Date createDate = systemConfiguration.getAuditSection().getDateCreated();
					Date lastDate = DateUtils.addDays(createDate, 30);
					Date currentDate = new Date();
					if (lastDate.getTime() > currentDate.getTime()){
						return true;
					}
				}
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SocketException e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping(value = "/admin/denied.html", method = RequestMethod.GET)
	public String displayDenied(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// logoff the user
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		if (auth != null) {
			new SecurityContextLogoutHandler().logout(request, response, auth);
			// new PersistentTokenBasedRememberMeServices().logout(request,
			// response, auth);
		}

		return "admin/logon";

	}

	@RequestMapping(value = "/admin/unauthorized.html", method = RequestMethod.GET)
	public String unauthorized(Model model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return "admin/unauthorized";
	}

}
